<?php
/**
 * Functions for handling how comments are displayed and used on the site. This allows more
 * precise control over their display and makes more filter and action hooks available to developers
 * to use in their customizations.
 *
 * @package Hybrid
 * @subpackage Functions
 */

/**
 * Arguments for the wp_list_comments_function() used in comments.php. Users can set up a 
 * custom comments callback function by changing $callback to the custom function.  Note that 
 * $style should remain 'ol' since this is hardcoded into the theme and is the semantically correct
 * element to use for listing comments.
 *
 * @since 0.7
 * @return array $args Arguments for listing comments.
 */
function hybrid_list_comments_args() {
	$args = array( 'style' => 'ol', 'type' => 'all', 'callback' => 'hybrid_comments_callback', 'end-callback' => 'hybrid_comments_end_callback' );
	return apply_atomic( 'list_comments_args', $args );
}

/**
 * Uses the $comment_type to determine which comment template should be used. Once the 
 * template is located, it is loaded for use. Child themes can create custom templates based off
 * the $comment_type. The comment template hierarchy is comment-$comment_type.php, 
 * comment.php.
 *
 * The templates are saved in $hybrid->templates[comment_template], so each comment template
 * is only located once if it is needed. Following comments will use the saved template.
 *
 * @since 0.2.3
 * @param $comment The comment variable
 * @param $args Array of arguments passed from wp_list_comments()
 * @param $depth What level the particular comment is
 */
function hybrid_comments_callback( $comment, $args, $depth ) {
	global $hybrid;

	$GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;

	$comment_type = get_comment_type();

	if ( !is_array( $hybrid->templates['comment_template'] ) || !array_key_exists( $comment_type, $hybrid->templates['comment_template'] ) ) {

		$templates = array( "comment-{$comment_type}.php", 'comment.php' );

		$hybrid->templates['comment_template'][$comment_type] = locate_template( $templates );
	}

	require( $hybrid->templates['comment_template'][$comment_type] );
}

/**
 * Ends the display of individual comments. Uses the callback parameter for wp_list_comments(). 
 * Needs to be used in conjunction with hybrid_comments_callback(). Not needed but used just in 
 * case something is changed.
 *
 * @since 0.2.3
 */
function hybrid_comments_end_callback() {
	echo '</li><!-- .comment -->';
}

/**
 * Displays the avatar for the comment author and wraps it in the comment author's URL if it is
 * available.  Adds a call to THEME_IMAGES . "/{$comment_type}.png" for the default avatars for
 * trackbacks and pingbacks.
 *
 * @since 0.2
 * @global $comment The current comment's DB object.
 * @global $hybrid The global Hybrid object.
 */
function hybrid_avatar() {
	global $comment, $hybrid;

	if ( !get_option( 'show_avatars' ) )
		return false;

	/* Get/set some comment variables. */
	$comment_type = get_comment_type();
	$author = esc_html( get_comment_author() );
	$url = esc_url( get_comment_author_url() );

	if ( 'pingback' == $comment_type || 'trackback' == $comment_type ) 
		$default_avatar = THEME_IMAGES . "/{$comment_type}.png";

	$default_avatar = apply_filters( "{$hybrid->prefix}_{$comment_type}_avatar", $default_avatar );

	$avatar = get_avatar( get_comment_author_email(), '80', $default_avatar, $author );

	/* If URL input, wrap avatar in hyperlink. */
	if ( $url )
		$avatar = '<a href="' . $url . '" rel="external nofollow" title="' . $author . '">' . $avatar . '</a>';

	echo apply_filters( "{$hybrid->prefix}_avatar", $avatar );
}

/**
 * Function for displaying a comment's metadata.
 *
 * @since 0.7
 * @param string $metadata Custom metadata to use.
 * @global $comment The global comment object.
 * @global $post The global post object.
 */
function hybrid_comment_meta( $metadata = '' ) {
	global $comment, $post;

	if ( !$metadata )
		$metadata = '[comment-author] [comment-published] [comment-permalink before="| "] [comment-edit-link before="| "] [comment-reply-link before="| "]';

	$metadata = '<div class="comment-meta comment-meta-data">' . $metadata . '</div>';

	echo do_shortcode( apply_filters( hybrid_get_prefix() . '_comment_meta', $metadata ) );
}

/**
 * Properly displays comment author name/link.  bbPress and other external
 * systems sometimes don't set a display name for registrations. WP has problems
 * if no display name is set. WP gives registered users URL of 'http://' if none is set.
 *
 * @since 0.2.2
 * @global $comment The current comment's DB object.
 * @global $hybrid The global theme object.
 * @return string
 */
function hybrid_comment_author() {
	global $comment, $hybrid;

	$author = esc_html( get_comment_author() );
	$url = esc_url( get_comment_author_url() );

	/* Display link and cite if URL is set. Also, properly cites trackbacks/pingbacks. */
	if ( $url )
		$output = '<cite class="fn" title="' . $url . '"><a href="' . $url . '" title="' . $author . '" class="url" rel="external nofollow">' . $author . '</a></cite>';
	else
		$output = '<cite class="fn">' . $author . '</cite>';

	$output = '<div class="comment-author vcard">' . apply_filters( 'get_comment_author_link', $output ) . '</div><!-- .comment-author .vcard -->';

	return apply_filters( "{$hybrid->prefix}_comment_author", $output );
}

?>